/*
 *  hellApp.m
 *  touchhell
 *
 *  Created by omega on 08/12/22.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */


#import "touchHell.h"
#import "hellApp.h"
#import "GameState.h"

MainState g_mainstate = MAINSTATE_NONE;
TextureFontParam fontparam[HELL_FONTPARAM_MAX];

void Hell_initLoad(){
	srand(time(NULL));
	
	Hell_loadTextureFromFile( HELL_TEXTURE_FONT, "font.png" );
	Hell_loadTextureFromFile( HELL_TEXTURE_FONTLARGE , "large_font.png");
	Hell_loadTextureFromFile( HELL_TEXTURE_FONTBOX , "box_font.png" );
	Hell_loadTextureFromFile( HELL_TEXTURE_BOX , "box.png" );
	Hell_loadTextureFromFile( HELL_TEXTURE_BACKGOUND , "background.png" );

	Hell_loadSoundEffectFromFile( HELL_SOUND_DROP , "jump.wav");
	Hell_loadSoundEffectFromFile( HELL_SOUND_FIRE, "se1.wav" );
	
	// init texture font params
	fontparam[ HELL_FONTPARAM_DEFAULT ].tex_w = 8.0f;
	fontparam[ HELL_FONTPARAM_DEFAULT ].tex_h = 8.0f;
	fontparam[ HELL_FONTPARAM_DEFAULT ].draw_w = 8.0f;
	fontparam[ HELL_FONTPARAM_DEFAULT ].draw_w = 8.0f;
	fontparam[ HELL_FONTPARAM_DEFAULT ].tex_idx = HELL_TEXTURE_FONT;
	fontparam[ HELL_FONTPARAM_DEFAULT ].column = 16;
	fontparam[ HELL_FONTPARAM_LARGE ].tex_w = 16.0f;
	fontparam[ HELL_FONTPARAM_LARGE ].tex_h = 32.0f;
	fontparam[ HELL_FONTPARAM_LARGE ].draw_w = 16.0f;
	fontparam[ HELL_FONTPARAM_LARGE ].draw_h = 32.0f;
	fontparam[ HELL_FONTPARAM_LARGE ].tex_idx = HELL_TEXTURE_FONTLARGE;
	fontparam[ HELL_FONTPARAM_LARGE ].column = 16;
	fontparam[ HELL_FONTPARAM_BOX ].tex_w = 32.0f;
	fontparam[ HELL_FONTPARAM_BOX ].tex_h = 32.0f;
	fontparam[ HELL_FONTPARAM_BOX ].draw_w = 32.0f;
	fontparam[ HELL_FONTPARAM_BOX ].draw_h = 32.0f;
	fontparam[ HELL_FONTPARAM_BOX ].tex_idx = HELL_TEXTURE_FONTBOX;
	fontparam[ HELL_FONTPARAM_BOX ].column = 16;
	
	
	if( Hell_loadBytes( "_GameState", &g_mainstate , sizeof(int) ) == TRUE ){
		NSLog(@"load mainstate");
		
		bool result = FALSE;
		MainState_setNextState( g_mainstate );
		
		switch( g_mainstate ){
			case MAINSTATE_GAME:
				result = GameState_resumeState();
				break;
			default:
				break;
		}
		
		if( !result ) MainState_setNextState( MAINSTATE_GAME );
	}else{
		MainState_setNextState( MAINSTATE_GAME );
	}	
}

void Hell_update(){
	switch( g_mainstate ){
		case MAINSTATE_GAME:
			GameState_update();
		default:
			break;
	}
}
void Hell_draw(){
	switch( g_mainstate ){
		case MAINSTATE_GAME:
			GameState_draw();
		Default:
			break;
	}
}
void Hell_terminate(){
	switch ( g_mainstate ) {
		case MAINSTATE_GAME:
			GameState_saveState();
			break;
		default:
			break;
	}
	Hell_saveBytes( "_GameState", &g_mainstate , sizeof(int) );
	Hell_synchronize();
}
void Hell_resignActive(){
}
void Hell_becomeActive(){
}

void Hell_callbackTouchBegan( Hell_TouchEvent* touchevents , int eventCount ){
	switch( g_mainstate ){
		case MAINSTATE_GAME:
			GameState_callbackTouchBegan( touchevents, eventCount );
			break;
		Default:
			break;
	}
}
void Hell_callbackTouchMoved( Hell_TouchEvent* touchevents , int eventCount ){
	switch( g_mainstate ){
		case MAINSTATE_GAME:
			GameState_callbackTouchMoved( touchevents, eventCount );
			break;
		Default:
			break;
	}
}
void Hell_callbackTouchEnded( Hell_TouchEvent* touchevents , int eventCount ){
	switch( g_mainstate ){
		case MAINSTATE_GAME:
			GameState_callbackTouchEnded( touchevents, eventCount );
			break;
		Default:
			break;
	}
}

//-----------------------------------------------

void MainState_setNextState( MainState next_mainstate ){
	switch( next_mainstate ){
		case MAINSTATE_TITLE:
			break;
		case MAINSTATE_GAME:
			GameState_initLoad();
			break;
		Default:
			Hell_logwrite( "Unknown mainstate" );
			break;
	}
	
	g_mainstate = next_mainstate;
}